<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>列表过滤</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<style>
    th,
    td {
        border: 1px solid black;
    }
</style>

<body>

    <div id="app">
        <h1>{{msg}}</h1>

        <input v-model="keyword" type="text" placeholder="请输入搜索关键字" /> <br /><br />


        <table>
            <tr>
                <th>序号</th>
                <th>英雄</th>
                <th>能量值</th>
                <th>选择</th>
            </tr>
            <tr v-for="(hero,index) in filterArr" :key="hero.id">
                <td>{{index+1}}</td>
                <td>{{hero.name}}</td>
                <td>{{hero.power}}</td>
                <td><input type="checkbox" /></td>
            </tr>
        </table>


    </div>

    <script>
        const vm = new Vue({
            el: '#app',
            data: {
                msg: '列表过滤',
                keyword: '',
                heros: [
                    { id: 101, name: '艾格文', power: 10000 },
                    { id: 102, name: '麦迪文', power: 9000 },
                    { id: 103, name: '古尔丹', power: 8000 },
                    { id: 104, name: '萨尔', power: 6000 },
                ],
                filterArr: []
            },
            watch: {
                keyword: {
                    immediate: true,
                    handler(val) {
                        //执行过滤规则
                        this.filterArr = this.heros.filter((hero) => {
                            return hero.name.indexOf(val) >= 0
                        })
                    }
                }
            }
        })





        // 回顾filter
        let arr = [1, 2, 3, 4, 5, 6, 7, 8]

        // filter不会破坏原数组的结构，会生成一个新的数组
        let newArr = arr.filter((num) => {
            // return 过滤规则
            return num > 5
        })

        console.log(newArr);
    </script>

</body>

</html>